package gqz.skin;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.zhy.changeskin.SkinManager;
import com.zhy.changeskin.callback.ISkinChangingCallback;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import gqz.skin.adapter.ListViewAdapter;

/**
 * 换肤Demo，参考zhy博客，使用zhy的开源库changeskin，支持应用内换肤和外部皮肤包换肤
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {

    String TAG = "MainActivity::";

    String skinName = "skin_night-release.apk";
    String mSkinPkgPath = Environment.getExternalStorageDirectory() + File.separator + skinName;

    boolean isNight = false;

    DrawerLayout dl;
    ViewFlipper flipper;
    View view_titlebar;
    TextView tv_title;
    ListView lv;
    GridView gv;
    Button btn_menu1, btn_menu2, btn_menu3, btn_back, btn_menu;

    //    HashMap<String, Integer> data = new HashMap<>();
    ArrayList textlist = new ArrayList();
    ArrayList iconlist = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        copyTo(skinName, Environment.getExternalStorageDirectory().getAbsolutePath(), skinName);
        initView();
        initData();
        bindData();
    }

    private void initView() {
        dl = (DrawerLayout) findViewById(R.id.main_dl);
        flipper = (ViewFlipper) findViewById(R.id.main_flipper);
        flipper.setDisplayedChild(0);
        view_titlebar = findViewById(R.id.main_titlebar);
        tv_title = (TextView) view_titlebar.findViewById(R.id.titlebar_tv_title);
        tv_title.setText("ListView");
        btn_back = (Button) view_titlebar.findViewById(R.id.titlebar_btn_back);
        btn_menu = (Button) view_titlebar.findViewById(R.id.titlebar_btn_menu);
        btn_back.setVisibility(View.INVISIBLE);
        btn_menu.setVisibility(View.INVISIBLE);
        btn_menu1 = (Button) findViewById(R.id.main_menu_btn1);
        btn_menu2 = (Button) findViewById(R.id.main_menu_btn2);
        btn_menu3 = (Button) findViewById(R.id.main_menu_btn3);
        btn_back.setOnClickListener(this);
        btn_menu.setOnClickListener(this);
        btn_menu1.setOnClickListener(this);
        btn_menu2.setOnClickListener(this);
        btn_menu3.setOnClickListener(this);
        lv = (ListView) findViewById(R.id.main_list);
        gv = (GridView) findViewById(R.id.main_grid);
    }

    private void initData() {
        String text1 = "子曰：“学而时习之，不亦说乎？有朋自远方来，不亦乐乎？人不知而不愠，不亦君子乎？”" +
                "有子曰：“其为人也孝弟，而好犯上者，鲜矣；不好犯上而好作乱者，未之有也。君子务本，本立而道生。孝弟也者，其为仁之本与！”" +
                "子曰：“巧言令色，鲜矣仁！”曾子曰：“吾日三省吾身：为人谋而不忠乎？与朋友交而不信乎？传不习乎？”" +
                "子曰：“道千乘之国，敬事而信，节用而爱人，使民以时。”" +
                "子曰：“弟子入则孝，出则弟，谨而信，泛爱众，而亲仁，行有余力，则以学文。”" +
                "子夏曰：“贤贤易色；事父母，能竭其力；事君，能致其身；与朋友交，言而有信。虽曰未学，吾必谓之学矣。”" +
                "子曰：“君子不重则不威，学则不固。主忠信，无友不如己者，过，则勿惮改。”" + "曾子曰：“慎终追远，民德归厚矣。”",

                text2 = "子禽问于子贡曰：“夫子至于是邦也，必闻其政，求之与，抑与之与？”子贡曰：“夫子温、良、恭、俭、让以得之。夫子之求之也，其诸异乎人之求之与？”" +
                        "子曰：“父在，观其志；父没，观其行；三年无改于父之道，可谓孝矣。”" +
                        "有子曰：“礼之用，和为贵。先王之道，斯为美，小大由之。有所不行，知和而和，不以礼节之，亦不可行也。”" +
                        "有子曰：“信近于义，言可复也。恭近于礼，远耻辱也。因不失其亲，亦可宗也。”" +
                        "子曰：“君子食无求饱，居无求安，敏于事而慎于言，就有道而正焉。可谓好学也已。”" +
                        "子贡曰：“贫而无谄，富而无骄，何如？”子曰：“可也。未若贫而乐，富而好礼者也。”子贡曰：“《诗》云：‘如切如磋，如琢如磨’，其斯之谓与？”" +
                        "子曰：“赐也，始可与言《诗》已矣，告诸往而知来者。”子曰：“不患人之不己知，患不知人也。”";

        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                textlist.add(text1);
                iconlist.add(R.mipmap.ic_launcher);
            } else {
                textlist.add(text2);
                iconlist.add(R.mipmap.ic_launcher_round);
            }
        }
    }

    private void bindData() {
        ListViewAdapter adapter = new ListViewAdapter(this, textlist, iconlist);
        adapter.setType(adapter.type_listview);
        lv.setAdapter(adapter);
        ListViewAdapter adapter2 = new ListViewAdapter(this, textlist, iconlist);
        adapter2.setType(adapter2.type_gridview);
        gv.setAdapter(adapter2);
    }

    // 复制文件
    private void copyTo(String fromPath, String toPath, String fileName) {
        // 复制位置
        // toPath：mnt/sdcard/lcuhg/health/
        // mnt/sdcard：表示sdcard
        File toFile = new File(toPath);
        // 如果不存在，创建文件夹
        if (!toFile.exists()) {
            boolean isCreate = toFile.mkdirs();
            // 打印创建结果
            Log.i("create dir", String.valueOf(isCreate));
        }

        try {
            // 根据文件名获取assets文件夹下的该文件的inputstream
            InputStream fromFileIs = getResources().getAssets().open(fromPath);
            int length = fromFileIs.available(); // 获取文件的字节数
            byte[] buffer = new byte[length]; // 创建byte数组
            FileOutputStream fileOutputStream = new FileOutputStream(toFile
                    + "/" + fileName); // 字节输入流
            BufferedInputStream bufferedInputStream = new BufferedInputStream(
                    fromFileIs);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    fileOutputStream);
            int len = bufferedInputStream.read(buffer);
            while (len != -1) {
                bufferedOutputStream.write(buffer, 0, len);
                len = bufferedInputStream.read(buffer);
            }
            bufferedInputStream.close();
            bufferedOutputStream.close();
            fromFileIs.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_menu_btn1:
                flipper.setDisplayedChild(0);
                tv_title.setText("ListView");
                break;
            case R.id.main_menu_btn2:
                flipper.setDisplayedChild(1);
                tv_title.setText("GridView");
                break;
            case R.id.main_menu_btn3:
                if (isNight) {
                    isNight = false;
                    SkinManager.getInstance().removeAnySkin();//重置为初始状态
                } else {
                    isNight = true;
                    //换肤
                    SkinManager.getInstance().changeSkin(mSkinPkgPath, "gqz.skin_night", new ISkinChangingCallback() {
                        @Override
                        public void onStart() {
                        }

                        @Override
                        public void onError(Exception e) {
//                            Toast.makeText(MainActivity.this, "换肤失败", Toast.LENGTH_SHORT).show();
                            Log.d(TAG, "换肤失败");
                        }

                        @Override
                        public void onComplete() {
//                            Toast.makeText(MainActivity.this, "换肤成功", Toast.LENGTH_SHORT).show();
                            Log.d(TAG, "换肤成功");
                        }
                    });
                }
                break;
        }
        dl.closeDrawer(Gravity.LEFT);
    }
}
